package com.nineclock.document.controller;

import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.document.dto.*;
import com.nineclock.document.service.DocCollaborationsService;
import com.nineclock.document.service.DocFileHistoryService;
import com.nineclock.document.service.DocFileService;
import com.nineclock.document.service.DocFolderService;
import com.nineclock.system.dto.SysUserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.asm.Advice;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(value = "APP文件管理接口", tags = "文档服务")
public class DocumentController {

    @Autowired
    private DocFileService docFileService;
    @Autowired
    private DocFolderService folderService;
    @Autowired
    private DocCollaborationsService collaborationsService;
    @Autowired
    private DocFileHistoryService docFileHistoryService;

    /**
     * 查询当前目录下的文件夹及文件
     * 接口路径：GET/document/listFolderAndFile
     */
    @GetMapping("listFolderAndFile")
    @ApiOperation("查询当前目录下的文件夹及文件")
    public Result<DocumentDto> queryFolderAndFile(@RequestParam(defaultValue = "0") Long parentFoldId){
        DocumentDto documentDto = docFileService.queryFolderAndFile(parentFoldId);
        return Result.success(documentDto);
    }

    /**
     * 新增文件夹
     * 接口路径：POST/document/insertFolder
     */
    @PostMapping("insertFolder")
    @ApiOperation("新增文件夹")
    public Result insertFolder(@RequestBody DocFolderDto folderDto){
        folderService.insertFolder(folderDto);
        return Result.success();
    }

    /**
     * 根据文档ID查询文档
     * 接口路径：GET/document/getFileByFileId
     */
    @GetMapping("getFileByFileId")
    @ApiOperation("根据文档ID查询文档")
    public Result<DocFileDto> getFileByFileId(Long id){
        DocFileDto fileDto = docFileService.getFileByFileId(id);
        return Result.success(fileDto);
    }

    /**
     * 查询文档协作者列表
     * 接口路径：GET/document/pagingCollaborations
     */
    @ApiOperation("查询文档协作者列表")
    @GetMapping("pagingCollaborations")
    public Result<PageResult> pagingCollaborations(Long id,
                                                   @RequestParam(defaultValue = "1") Integer page,
                                                   @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult pageResult = collaborationsService.pagingCollaborations(id, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 根据文件ID获取员工列表
     * 接口路径：GET/document/pagingUsers
     */
    @GetMapping("pagingUsers")
    @ApiOperation("根据文件ID获取员工列表")
    public Result<List<DocUserCollaborationDTO>> queryUsers(Long id){
        List<DocUserCollaborationDTO> dtoList = docFileService.queryUsers(id);
        return Result.success(dtoList);
    }
    /**
     * 封禁/解封文件
     * 接口路径：PUT/document/{fileId}/{status}*/
    @PutMapping("/{fileId}/{status}")
    @ApiOperation("封禁/解封文件")
    public Result updateStatus(@PathVariable Long fileId,@PathVariable Short status){
        docFileService.updateStatus(fileId,status);
        return Result.success();

    }



    //查询文件历史记录
    //接口路径：GET/document/history/{fileId}
    @ApiOperation("查询文件历史记录")
    @GetMapping("history/{fileId}")
    public Result<List<DocFileHistoryDto>> queryHistory(@PathVariable("fileId") Long fileId){
        List<DocFileHistoryDto> dtoList = docFileHistoryService.queryHistory(fileId);
        return Result.success(dtoList);
    }


    //2021.12.29
    /**
     * 新增文件
     * 接口路径：POST/document/insertFile
     */
    @ApiOperation("新增文件")
    @PostMapping("insertFile")
    public Result insertFile(@RequestBody DocFileDto fileDto){
        docFileService.insertFile(fileDto);
        return Result.success();
    }


    /**
     * 根据ID更新文档(前端忘记传id过来)
     * 接口路径：POST/document/updateFile
     */
    @ApiOperation("更新文档")
    @PostMapping("updateFile")
    public Result updateFile(@RequestBody DocFileDto fileDto){
        docFileService.updateFile(fileDto);
        return Result.success();
    }

    /**
     * 添加文档协作者信息
     * 接口路径：POST/document/insertCollaboration
     */
    @ApiOperation("添加文档协作者信息")
    @PostMapping("insertCollaboration")
    public Result insertCollaboration(@RequestBody Map<String, Long> map){
        Long fileId = Long.valueOf(map.get("fileId").toString());
        Long companyUserId = Long.valueOf(map.get("userId").toString());
        collaborationsService.insertCollaboration(fileId,companyUserId);
        return Result.success();
    }

    /**
     * 删除文档协作者信息
     * 接口路径：POST/document/deleteCollaboration
     */
    @ApiOperation("删除文档协作者信息")
    @PostMapping("deleteCollaboration")
    public Result deleteCollaboration(@RequestBody Map<String, Long> map){
        Long fileId = Long.valueOf(map.get("fileId").toString());
        Long companyUserId = Long.valueOf(map.get("userId").toString());
        collaborationsService.deleteCollaboration(fileId,companyUserId);
        return Result.success();
    }

    /**
     * 分页查询文档列表
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    /**
     * 分页查询文件列表
     * 接口路径：GET/document/pagingFile*/
    @GetMapping("pagingFile")
    @ApiOperation("分页查询文件列表")
    public Result<PageResult<DocFileDto>> selectDocFile(@RequestParam(required = false) String keyword,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<DocFileDto> docFile=docFileService.selectDocFile(keyword,page,pageSize);
        return Result.success(docFile);
    }


    /**
     * 分页查询员工
     * */
    @GetMapping("pagingPersonnel")
    @ApiOperation("分页查询员工")
    public Result<PageResult<SysUserDTO>> selectDocPersonnel(@RequestParam(required = false) String keyword, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<SysUserDTO> sysUserDTOPageResult=  docFileService.selectDocPersonnel(keyword,page,pageSize);
        return Result.success(sysUserDTOPageResult);
    }


    @PutMapping("/id/{personnelId}/{status}")
    @ApiOperation("正常/离职")
    public Result updatastatus(@PathVariable Long personnelId,@PathVariable Short status){
        docFileService.updateStatu(personnelId,status);
        return Result.success();
    }
}

